<template>
    <div class="shopping-index">
        <!-- 轮播图banner -->
        <div class="banner-section">
            <el-carousel height="300px" :interval="4000" arrow="hover">
                <el-carousel-item v-for="item in banners" :key="item.id">
                    <div class="banner-item" :style="{ backgroundImage: `url(${item.image})` }">
                        <div class="banner-content">
                            <h2 class="banner-title">{{ item.title }}</h2>
                            <p class="banner-subtitle">{{ item.subtitle }}</p>
                            <el-button type="primary" size="large" round class="banner-btn">
                                立即购买
                            </el-button>
                        </div>
                    </div>
                </el-carousel-item>
            </el-carousel>
        </div>

        <!-- 商品分类 -->
        <div class="category-section">
            <div class="section-header">
                <h2 class="section-title">商品分类</h2>
                <p class="section-subtitle">精选优质商品分类</p>
            </div>
            
            <div class="category-grid">
                <div 
                    v-for="category in categories" 
                    :key="category.id"
                    class="category-card"
                    @click="goToCategory(category.id)"
                >
                    <div class="category-icon">
                        <el-icon :size="40">
                            <component :is="category.icon" />
                        </el-icon>
                    </div>
                    <h3 class="category-name">{{ category.name }}</h3>
                </div>
            </div>
        </div>

        <!-- 优选商品 -->
        <div class="featured-section">
            <div class="section-header">
                <h2 class="section-title">优选商品</h2>
                <p class="section-subtitle">为您精心挑选的热门商品</p>
            </div>
            
            <div class="product-grid">
                <div 
                    v-for="product in featuredProducts" 
                    :key="product.id"
                    class="product-card"
                    @click="goToProduct(product.id)"
                >
                    <div class="product-image">
                        <img :src="product.image" :alt="product.name" />
                        <div class="product-overlay">
                            <el-button type="primary" circle>
                                <el-icon><View /></el-icon>
                            </el-button>
                            <el-button type="success" circle>
                                <el-icon><ShoppingCart /></el-icon>
                            </el-button>
                        </div>
                    </div>
                    <div class="product-info">
                        <h3 class="product-name">{{ product.name }}</h3>
                        <p class="product-desc">{{ product.description }}</p>
                        <div class="product-price">
                            <span class="current-price">¥{{ product.price }}</span>
                            <span v-if="product.originalPrice" class="original-price">¥{{ product.originalPrice }}</span>
                        </div>
                        <div class="product-stats">
                            <span class="sales">已售{{ product.sales }}件</span>
                            <div class="rating">
                                <el-rate 
                                    v-model="product.rating" 
                                    disabled 
                                    size="small"
                                    show-score
                                    score-template="{value}"
                                />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 热销榜单 -->
        <div class="hot-sales-section"> 
            <div class="section-header">
                <h2 class="section-title">热销榜单</h2>
                <p class="section-subtitle">最受欢迎的商品排行</p>
            </div>
            
            <div class="hot-sales-list">
                <div 
                    v-for="(product, index) in hotSales" 
                    :key="product.id"
                    class="hot-sales-item"
                    @click="goToProduct(product.id)"
                >
                    <div class="rank-badge" :class="{ 'top-three': index < 3 }">
                        {{ index + 1 }}
                    </div>
                    <div class="product-thumb">
                        <img :src="product.image" :alt="product.name" />
                    </div>
                    <div class="product-details">
                        <h4 class="product-name">{{ product.name }}</h4>
                        <p class="product-price">¥{{ product.price }}</p>
                        <div class="product-meta">
                            <span class="sales">月销{{ product.monthlySales }}+</span>
                            <span class="rating">{{ product.rating }}分</span>
                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
    ShoppingCart, 
    View, 
    Goods, 
    Tools, 
    Phone, 
    House, 
    Trophy 
} from '@element-plus/icons-vue'
import { getClassifications, getOptimization } from '@/api/shopping'
import banner1 from '@/assets/imgs/banner1.webp'
import banner2 from '@/assets/imgs/banner2.webp'
import banner3 from '@/assets/imgs/banner3.webp'

const router = useRouter()

// 轮播图数据
const banners = ref([
    {
        id: 1,
        title: '春季新品大促',
        subtitle: '全场8折起，限时优惠',
        image: banner1
    },
    {
        id: 2,
        title: '精选好货',
        subtitle: '品质保证，放心购买',
        image: banner2
    },
    {
        id: 3,
        title: '会员专享',
        subtitle: '专属优惠，更多特权',
        image: banner3
    }
])

// 商品分类数据
const categories = ref([
    { id: 1, name: '数码产品', icon: 'Tools' },
    { id: 2, name: '手机配件', icon: 'Phone' },
    { id: 3, name: '家居用品', icon: 'House' },
    { id: 4, name: '运动户外', icon: 'Trophy' },
    { id: 5, name: '服装鞋包', icon: 'Goods' },
    { id: 6, name: '美妆护肤', icon: 'Goods' }
])

// 优选商品数据
const featuredProducts = ref([])

// 热销商品数据
const hotSales = ref([])

// 获取商品分类
const fetchCategories = async () => {
    try {
        const data = await getClassifications()
        if (data && data.length > 0) {
            categories.value = data.map(item => ({
                ...item,
                icon: 'Goods'
            }))
        }
    } catch (error) {
        console.error('获取分类失败:', error)
    }
}

// 获取优选商品
const fetchFeaturedProducts = async () => {
    try {
        const data = await getOptimization()
        if (data && data.length > 0) {
            featuredProducts.value = data.map(item => ({
                id: item.id,
                name: item.name,
                description: item.description || '暂无描述',
                price: item.price,
                originalPrice: item.original_price,
                image: item.commodityimg_set && item.commodityimg_set.length > 0 ? item.commodityimg_set[0].src : '/placeholder.jpg',
                rating: Math.random() * 2 + 3, // 3-5分随机评分
                sales: Math.floor(Math.random() * 1000) + 100
            }))
        }
    } catch (error) {
        console.error('获取优选商品失败:', error)
    }
}
// 获取热销商品
const hotSalesProducts = async () => {
    try {
        const data = await getOptimization()
        if (data && data.length > 0) {
            hotSales.value = data.map(item => ({
                id: item.id,
                name: item.name,
                description: item.description || '暂无描述',
                price: item.price,
                originalPrice: item.original_price,
                image: item.commodityimg_set && item.commodityimg_set.length > 0 ? item.commodityimg_set[0].src : '/placeholder.jpg',
                rating: Math.random() * 2 + 3, // 3-5分随机评分
                sales: Math.floor(Math.random() * 1000) + 100
            }))
        }
    } catch (error) {
        console.error('获取优选商品失败:', error)
    }
}

// 跳转到分类页面
const goToCategory = (categoryId) => {
    router.push({ name: 'ShoppingList', params: { id: categoryId } })
}

// 跳转到商品详情页
const goToProduct = (productId) => {
    router.push({ name: 'shopping_detail', params: { id: productId } })
}

onMounted(() => {
    fetchCategories()
    fetchFeaturedProducts()
    hotSalesProducts()
    
    // 模拟热销数据
    // hotSales.value = [
    //     { id: 1, name: '智能手机', price: 2999, monthlySales: 1200, rating: 4.8, image: '/placeholder.jpg' },
    //     { id: 2, name: '无线耳机', price: 299, monthlySales: 800, rating: 4.6, image: '/placeholder.jpg' },
    //     { id: 3, name: '智能手表', price: 1599, monthlySales: 600, rating: 4.7, image: '/placeholder.jpg' },
    //     { id: 4, name: '平板电脑', price: 2199, monthlySales: 450, rating: 4.5, image: '/placeholder.jpg' },
    //     { id: 5, name: '游戏手柄', price: 199, monthlySales: 380, rating: 4.4, image: '/placeholder.jpg' }
    // ]
})
</script>

<style lang="less" scoped>
.shopping-index {
    padding: 0;
}

.banner-section {
    margin-bottom: 40px;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    
    .banner-item {
        height: 300px;
        background-size: cover;
        background-position: center;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        
        &::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(45deg, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.2));
        }
    }
    
    .banner-content {
        text-align: center;
        color: white;
        position: relative;
        z-index: 1;
        
        .banner-title {
            font-size: 36px;
            font-weight: 700;
            margin-bottom: 12px;
        }
        
        .banner-subtitle {
            font-size: 18px;
            margin-bottom: 24px;
            opacity: 0.9;
        }
        
        .banner-btn {
            background: linear-gradient(45deg, #667eea, #764ba2);
            border: none;
            padding: 12px 30px;
            font-size: 16px;
            
            &:hover {
                transform: translateY(-2px);
                box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
            }
        }
    }
}

.section-header {
    text-align: center;
    margin-bottom: 40px;
    
    .section-title {
        font-size: 28px;
        font-weight: 600;
        color: #2c3e50;
        margin-bottom: 8px;
    }
    
    .section-subtitle {
        color: #7f8c8d;
        font-size: 16px;
        margin: 0;
    }
}

.category-section {
    margin-bottom: 60px;
    
    .category-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 24px;
    }
    
    .category-card {
        background: white;
        border-radius: 16px;
        padding: 30px 20px;
        text-align: center;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        
        &:hover {
            transform: translateY(-8px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
        }
        
        .category-icon {
            width: 80px;
            height: 80px;
            margin: 0 auto 16px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
        
        .category-name {
            font-size: 18px;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 8px;
        }
        
        .category-count {
            color: #7f8c8d;
            font-size: 14px;
            margin: 0;
        }
    }
}

.featured-section {
    margin-bottom: 60px;
    
    .product-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 24px;
    }
    
    .product-card {
        background: white;
        border-radius: 16px;
        overflow: hidden;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        
        &:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
            
            .product-overlay {
                opacity: 1;
            }
        }
        
        .product-image {
            position: relative;
            height: 200px;
            overflow: hidden;
            
            img {
                width: 100%;
                height: 100%;
                object-fit: cover;
                transition: transform 0.3s ease;
            }
            
            .product-overlay {
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: rgba(0, 0, 0, 0.5);
                display: flex;
                align-items: center;
                justify-content: center;
                gap: 12px;
                opacity: 0;
                transition: opacity 0.3s ease;
            }
        }
        
        .product-info {
            padding: 20px;
            
            .product-name {
                font-size: 16px;
                font-weight: 600;
                color: #2c3e50;
                margin-bottom: 8px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            
            .product-desc {
                color: #7f8c8d;
                font-size: 14px;
                margin-bottom: 12px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            
            .product-price {
                margin-bottom: 12px;
                
                .current-price {
                    font-size: 20px;
                    font-weight: 600;
                    color: #e74c3c;
                }
                
                .original-price {
                    font-size: 14px;
                    color: #95a5a6;
                    text-decoration: line-through;
                    margin-left: 8px;
                }
            }
            
            .product-stats {
                display: flex;
                align-items: center;
                justify-content: space-between;
                
                .sales {
                    font-size: 12px;
                    color: #95a5a6;
                }
                
                .rating {
                    :deep(.el-rate__text) {
                        font-size: 12px;
                        color: #95a5a6;
                    }
                }
            }
        }
    }
}

.hot-sales-section {
    .hot-sales-list {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
        gap: 16px;
    }
    
    .hot-sales-item {
        background: white;
        border-radius: 12px;
        padding: 16px;
        display: flex;
        align-items: center;
        gap: 16px;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        
        &:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
        }
        
        .rank-badge {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: #ecf0f1;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            color: #7f8c8d;
            
            &.top-three {
                background: linear-gradient(45deg, #f39c12, #e74c3c);
                color: white;
            }
        }
        
        .product-thumb {
            width: 60px;
            height: 60px;
            border-radius: 8px;
            overflow: hidden;
            
            img {
                width: 100%;
                height: 100%;
                object-fit: cover;
            }
        }
        
        .product-details {
            flex: 1;
            
            .product-name {
                font-size: 16px;
                font-weight: 600;
                color: #2c3e50;
                margin-bottom: 4px;
            }
            
            .product-price {
                font-size: 18px;
                font-weight: 600;
                color: #e74c3c;
                margin-bottom: 4px;
            }
            
            .product-meta {
                display: flex;
                gap: 16px;
                font-size: 12px;
                color: #95a5a6;
            }
        }
    }
}

/* 响应式设计 */
@media (max-width: 768px) {
    .banner-content {
        .banner-title {
            font-size: 24px;
        }
        
        .banner-subtitle {
            font-size: 14px;
        }
    }
    
    .category-grid {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 16px;
    }
    
    .category-card {
        padding: 20px 15px;
        
        .category-icon {
            width: 60px;
            height: 60px;
        }
    }
    
    .product-grid {
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    }
    
    .hot-sales-list {
        grid-template-columns: 1fr;
    }
}
</style>